package commServer;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

/**
 * @author Niels
 *
 */
public class En_Decrypt {

	//Encryption method
	public static String xor_encrypt(String message) {
		try {
			if (message == null)
				return null;

			String key = "!1ugiA!";
			char[] keys = key.toCharArray();
			char[] mesg = message.toCharArray();
			BASE64Encoder encoder = new BASE64Encoder();

			int ml = mesg.length;
			int kl = keys.length;
			char[] newmsg = new char[ml];

			for (int i = 0; i < ml; i++) {
				newmsg[i] = (char) (mesg[i] ^ keys[i % kl]);
			}
			mesg = null;
			keys = null;
			String temp = new String(newmsg);
			return new String(new BASE64Encoder().encodeBuffer(temp.getBytes()));
		} catch (Exception e) {
			return null;
		}
	}
	//Decryption method
	public static String xor_decrypt(String message) {
		try {
			if (message == null)
				return null;
			BASE64Decoder decoder = new BASE64Decoder();
			String key = "!1ugiA!";
			char[] keys = key.toCharArray();
			message = new String(decoder.decodeBuffer(message));
			char[] mesg = message.toCharArray();

			int ml = mesg.length;
			int kl = keys.length;
			char[] newmsg = new char[ml];

			for (int i = 0; i < ml; i++) {
				newmsg[i] = (char) (mesg[i] ^ keys[i % kl]);
			}
			mesg = null;
			keys = null;
			return new String(newmsg);
		} catch (Exception e) {
			return null;
		}
	}
}